home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 July / CHIP 2006-07.2.iso / program / web_gelistirme / easyphp1-7_setup.exe / {app} / phpmyadmin / libraries / string.lib.php < prev    next >
Encoding:
PHP Script  |  2003-09-07  |  9.7 KB  |  326 lines

  1. <?php
  2. /* $Id: string.lib.php,v 1.10 2002/10/25 13:55:56 loic1 Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5.  
  6. /** Specialized String Functions for phpMyAdmin
  7.  *
  8.  * Copyright 2002 Robin Johnson <robbat2@users.sourceforge.net>
  9.  * http://www.orbis-terrarum.net/?l=people.robbat2
  10.  *
  11.  * Defines a set of function callbacks that have a pure C version available if
  12.  * the "ctype" extension is available, but otherwise have PHP versions to use
  13.  * (that are slower).
  14.  *
  15.  * The SQL Parser code relies heavily on these functions.
  16.  */
  17.  
  18.  
  19. if (!defined('PMA_STR_LIB_INCLUDED')) {
  20.     define('PMA_STR_LIB_INCLUDED', 1);
  21.  
  22.     // This is for handling input better
  23.     if (defined('PMA_MULTIBYTE_ENCODING')) {
  24.         $GLOBALS['PMA_strlen']  = 'mb_strlen';
  25.         $GLOBALS['PMA_strpos']  = 'mb_strpos';
  26.         $GLOBALS['PMA_strrpos'] = 'mb_strrpos';
  27.         $GLOBALS['PMA_substr']  = 'mb_substr';
  28.     } else {
  29.         $GLOBALS['PMA_strlen']  = 'strlen';
  30.         $GLOBALS['PMA_strpos']  = 'strpos';
  31.         $GLOBALS['PMA_strrpos'] = 'strrpos';
  32.         $GLOBALS['PMA_substr']  = 'substr';
  33.     }
  34.  
  35.  
  36.     /**
  37.      * This checks if a string actually exists inside another string
  38.      * We try to do it in a PHP3-portable way.
  39.      * We don't care about the position it is in.
  40.      *
  41.      * @param   string   string to search for
  42.      * @param   string   string to search in
  43.      *
  44.      * @return  boolean  whether the needle is in the haystack or not
  45.      */
  46.     function PMA_STR_strInStr($needle, $haystack)
  47.     {
  48.         // $GLOBALS['PMA_strpos']($haystack, $needle) !== FALSE
  49.         // return (is_integer($GLOBALS['PMA_strpos']($haystack, $needle)));
  50.         return $GLOBALS['PMA_strpos'](' ' . $haystack, $needle);
  51.     } // end of the "PMA_STR_strInStr()" function
  52.  
  53.  
  54.     /**
  55.      * Checks if a given character position in the string is escaped or not
  56.      *
  57.      * @param   string   string to check for
  58.      * @param   integer  the character to check for
  59.      * @param   integer  starting position in the string
  60.      *
  61.      * @return  boolean  whether the character is escaped or not
  62.      */
  63.     function PMA_STR_charIsEscaped($string, $pos, $start = 0)
  64.     {
  65.         $len = $GLOBALS['PMA_strlen']($string);
  66.         // Base case:
  67.         // Check for string length or invalid input or special case of input
  68.         // (pos == $start)
  69.         if (($pos == $start) || ($len <= $pos)) {
  70.             return FALSE;
  71.         }
  72.  
  73.         $p           = $pos - 1;
  74.         $escaped     = FALSE;
  75.         while (($p >= $start) && ($string[$p] == '\\')) {
  76.             $escaped = !$escaped;
  77.             $p--;
  78.         } // end while
  79.  
  80.         if ($pos < $start) {
  81.             // throw error about strings
  82.         }
  83.  
  84.         return $escaped;
  85.     } // end of the "PMA_STR_charIsEscaped()" function
  86.  
  87.  
  88.     /**
  89.      * Checks if a number is in a range
  90.      *
  91.      * @param   integer  number to check for
  92.      * @param   integer  lower bound
  93.      * @param   integer  upper bound
  94.      *
  95.      * @return  boolean  whether the number is in the range or not
  96.      */
  97.     function PMA_STR_numberInRangeInclusive($num, $lower, $upper)
  98.     {
  99.         return (($num >= $lower) && ($num <= $upper));
  100.     } // end of the "PMA_STR_numberInRangeInclusive()" function
  101.  
  102.  
  103.     /**
  104.      * Checks if a character is a digit
  105.      *
  106.      * @param   string   character to check for
  107.      *
  108.      * @return  boolean  whether the character is a digit or not
  109.      *
  110.      * @see     PMA_STR_numberInRangeInclusive()
  111.      */
  112.     function PMA_STR_isDigit($c)
  113.     {
  114.         $ord_zero = 48; //ord('0');
  115.         $ord_nine = 57; //ord('9');
  116.         $ord_c    = ord($c);
  117.  
  118.         return PMA_STR_numberInRangeInclusive($ord_c, $ord_zero, $ord_nine);
  119.     } // end of the "PMA_STR_isDigit()" function
  120.  
  121.  
  122.     /**
  123.      * Checks if a character is an hexadecimal digit
  124.      *
  125.      * @param   string   character to check for
  126.      *
  127.      * @return  boolean  whether the character is an hexadecimal digit or not
  128.      *
  129.      * @see     PMA_STR_numberInRangeInclusive()
  130.      */
  131.     function PMA_STR_isHexDigit($c)
  132.     {
  133.         $ord_Aupper = 65;  //ord('A');
  134.         $ord_Fupper = 70;  //ord('F');
  135.         $ord_Alower = 97;  //ord('a');
  136.         $ord_Flower = 102; //ord('f');
  137.         $ord_zero   = 48;  //ord('0');
  138.         $ord_nine   = 57;  //ord('9');
  139.         $ord_c      = ord($c);
  140.  
  141.         return (PMA_STR_numberInRangeInclusive($ord_c, $ord_zero, $ord_nine)
  142.                 || PMA_STR_numberInRangeInclusive($ord_c, $ord_Aupper, $ord_Fupper)
  143.                 || PMA_STR_numberInRangeInclusive($ord_c, $ord_Alower, $ord_Flower));
  144.     } // end of the "PMA_STR_isHexDigit()" function
  145.  
  146.  
  147.     /**
  148.      * Checks if a character is an upper alphabetic one
  149.      *
  150.      * @param   string   character to check for
  151.      *
  152.      * @return  boolean  whether the character is an upper alphabetic one or
  153.      *                   not
  154.      *
  155.      * @see     PMA_STR_numberInRangeInclusive()
  156.      */
  157.     function PMA_STR_isUpper($c)
  158.     {
  159.         $ord_zero = 65; //ord('A');
  160.         $ord_nine = 90; //ord('Z');
  161.         $ord_c    = ord($c);
  162.  
  163.         return PMA_STR_numberInRangeInclusive($ord_c, $ord_zero, $ord_nine);
  164.     } // end of the "PMA_STR_isUpper()" function
  165.  
  166.  
  167.     /**
  168.      * Checks if a character is a lower alphabetic one
  169.      *
  170.      * @param   string   character to check for
  171.      *
  172.      * @return  boolean  whether the character is a lower alphabetic one or
  173.      *                   not
  174.      *
  175.      * @see     PMA_STR_numberInRangeInclusive()
  176.      */
  177.     function PMA_STR_isLower($c)
  178.     {
  179.         $ord_zero = 97;  //ord('a');
  180.         $ord_nine = 122; //ord('z');
  181.         $ord_c    = ord($c);
  182.  
  183.         return PMA_STR_numberInRangeInclusive($ord_c, $ord_zero, $ord_nine);
  184.     } // end of the "PMA_STR_isLower()" function
  185.  
  186.  
  187.     /**
  188.      * Checks if a character is an alphabetic one
  189.      *
  190.      * @param   string   character to check for
  191.      *
  192.      * @return  boolean  whether the character is an alphabetic one or not
  193.      *
  194.      * @see     PMA_STR_isUpper()
  195.      * @see     PMA_STR_isLower()
  196.      */
  197.     function PMA_STR_isAlpha($c)
  198.     {
  199.         return (PMA_STR_isUpper($c) || PMA_STR_isLower($c));
  200.     } // end of the "PMA_STR_isAlpha()" function
  201.  
  202.  
  203.     /**
  204.      * Checks if a character is an alphanumeric one
  205.      *
  206.      * @param   string   character to check for
  207.      *
  208.      * @return  boolean  whether the character is an alphanumeric one or not
  209.      *
  210.      * @see     PMA_STR_isUpper()
  211.      * @see     PMA_STR_isLower()
  212.      * @see     PMA_STR_isDigit()
  213.      */
  214.     function PMA_STR_isAlnum($c)
  215.     {
  216.         return (PMA_STR_isUpper($c) || PMA_STR_isLower($c) || PMA_STR_isDigit($c));
  217.     } // end of the "PMA_STR_isAlnum()" function
  218.  
  219.  
  220.     /**
  221.      * Checks if a character is a space one
  222.      *
  223.      * @param   string   character to check for
  224.      *
  225.      * @return  boolean  whether the character is a space one or not
  226.      *
  227.      * @see     PMA_STR_numberInRangeInclusive()
  228.      */
  229.     function PMA_STR_isSpace($c)
  230.     {
  231.         $ord_space = 32; //ord(' ')
  232.         $ord_tab   = 9; //ord('\t')
  233.         $ord_CR    = 13; //ord('\n')
  234.         $ord_NOBR  = 160; //ord('U+00A0);
  235.         $ord_c     = ord($c);
  236.  
  237.         return (($ord_c == $ord_space)
  238.                 || ($ord_c == $ord_NOBR)
  239.                 || PMA_STR_numberInRangeInclusive($ord_c, $ord_tab, $ord_CR));
  240.     } // end of the "PMA_STR_isSpace()" function
  241.  
  242.  
  243.     /**
  244.      * Checks if a character is an accented character
  245.      *
  246.      * @note    Presently this only works for some character sets. More work
  247.      *          may be needed to fix it.
  248.      *
  249.      * @param   string   character to check for
  250.      *
  251.      * @return  boolean  whether the character is an upper alphabetic one or
  252.      *                   not
  253.      *
  254.      * @see     PMA_STR_numberInRangeInclusive()
  255.      */
  256.     function PMA_STR_isAccented($c)
  257.     {
  258.         $ord_min1 = 192; //ord('A');
  259.         $ord_max1 = 214; //ord('Z');
  260.         $ord_min2 = 216; //ord('A');
  261.         $ord_max2 = 246; //ord('Z');
  262.         $ord_min3 = 248; //ord('A');
  263.         $ord_max3 = 255; //ord('Z');
  264.  
  265.         $ord_c    = ord($c);
  266.  
  267.         return PMA_STR_numberInRangeInclusive($ord_c, $ord_min1, $ord_max1)
  268.                || PMA_STR_numberInRangeInclusive($ord_c, $ord_min2, $ord_max2)
  269.                || PMA_STR_numberInRangeInclusive($ord_c, $ord_min2, $ord_max2);
  270.     } // end of the "PMA_STR_isAccented()" function
  271.  
  272.  
  273.     /**
  274.      * Checks if a character is an SQL identifier
  275.      *
  276.      * @param   string   character to check for
  277.      * @param   boolean  whether the dot character is valid or not
  278.      *
  279.      * @return  boolean  whether the character is an SQL identifier or not
  280.      *
  281.      * @see     PMA_STR_isAlnum()
  282.      */
  283.     function PMA_STR_isSqlIdentifier($c, $dot_is_valid = FALSE)
  284.     {
  285.         return (PMA_STR_isAlnum($c)
  286.                 || PMA_STR_isAccented($c)
  287.                 || ($c == '_') || ($c == '$')
  288.                 || (($dot_is_valid != FALSE) && ($c == '.')));
  289.     } // end of the "PMA_STR_isSqlIdentifier()" function
  290.  
  291.  
  292.     /**
  293.      * Binary search of a value in a sorted array
  294.      *
  295.      * @param   string   string to search for
  296.      * @param   array    sorted array to search into
  297.      * @param   integer  size of sorted array to search into
  298.      *
  299.      * @return  boolean  whether the string has been found or not
  300.      */
  301.     function PMA_STR_binarySearchInArr($str, $arr, $arrsize)
  302.     {
  303.         // $arr NUST be sorted, due to binary search
  304.         $top    = $arrsize - 1;
  305.         $bottom = 0;
  306.         $found  = FALSE;
  307.  
  308.         while (($top >= $bottom) && ($found == FALSE)) {
  309.             $mid        = intval(($top + $bottom) / 2);
  310.             $res        = strcmp($str, $arr[$mid]);
  311.             if ($res == 0) {
  312.                 $found  = TRUE;
  313.             } else if ($res < 0) {
  314.                 $top    = $mid - 1;
  315.             } else {
  316.                 $bottom = $mid + 1;
  317.             }
  318.         } // end while
  319.  
  320.         return $found;
  321.     } // end of the "PMA_STR_binarySearchInArr()" function
  322.  
  323. } // $__PMA_STR_LIB__
  324.  
  325. ?>
  326.